/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Assignment.DAO;

import Assignment.DTO.OrderDTO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author dieptn
 */
public class OrderDAO {

    public static ArrayList search(Connection con, OrderDTO order) {
        ArrayList<OrderDTO> result = new ArrayList<OrderDTO>();
        String sql = "";
        PreparedStatement stm = null;
        try {
            if (order == null) {
                sql = "SELECT * FROM Orders";
                stm = con.prepareStatement(sql);
            }
            ResultSet rs = stm.executeQuery();
            OrderDTO orderTemp = null;
            while (rs.next()) {
                orderTemp = new OrderDTO();
                orderTemp.setOrderID(rs.getString(1));
                orderTemp.setUsername(rs.getString(2));
                orderTemp.setOrderDate(rs.getTimestamp(3));
                orderTemp.setDeliveryDate(rs.getTimestamp(4));
                orderTemp.setTotalPrice(rs.getFloat(5));
                orderTemp.setStatus(rs.getString(6));
                orderTemp.setNote(rs.getString(7));
                result.add(orderTemp);
            }
            stm.close();
            con.close();
        } catch (Exception ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public static int insert(Connection con, OrderDTO order) {
        int result = 0;
        try {
            String sql = "INSERT INTO Orders VALUES(?,?,?,?,?,?,?)";
            PreparedStatement stm = con.prepareStatement(sql);
            stm.setString(1, order.getOrderID());
            stm.setString(2, order.getUsername());
            stm.setTimestamp(3, new java.sql.Timestamp(order.getOrderDate().getTime()));
            stm.setTimestamp(4, new java.sql.Timestamp(order.getDeliveryDate().getTime()));
            stm.setFloat(5, order.getTotalPrice());
            stm.setString(6, order.getStatus());
            stm.setString(7, order.getNote());
            result = stm.executeUpdate();

            stm.close();
            //con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static int update(Connection con, OrderDTO order) {
        int result = 0;
        try {
            String query = "UPDATE Orders SET username=?, orderDate=?, "
                    + "deliveryDate=?, totalPrice=?, status=?, note=? "
                    + "WHERE orderID=?";
            PreparedStatement stm = con.prepareStatement(query);
            stm.setString(1, order.getUsername());
            stm.setTimestamp(2, new java.sql.Timestamp(order.getOrderDate().getTime()));
            stm.setTimestamp(3, new java.sql.Timestamp(order.getDeliveryDate().getTime()));
            stm.setFloat(4, order.getTotalPrice());
            stm.setString(5, order.getStatus());
            stm.setString(6, order.getNote());
            stm.setString(7, order.getOrderID());

            result = stm.executeUpdate();
            stm.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static int deleteByOrderID(Connection con, String orderID) {
        int result = 0;
        try {
            String sql = "DELETE FROM Orders WHERE orderID=?";
            PreparedStatement stm = con.prepareStatement(sql);
            stm.setString(1, orderID);
            result = stm.executeUpdate();
            stm.close();
            //con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static int deleteByUsername(Connection con, String username) {
        int result = 0;
        try {
            String sql = "DELETE FROM Orders WHERE username=?";
            PreparedStatement stm = con.prepareStatement(sql);
            stm.setString(1, username);
            result = stm.executeUpdate();
            stm.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
